<?php
/**
 * This file is part of DomAr.
 * 
 * DomAr is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * DomAr is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with DomAr.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @link http://inditel.ee/
 * @copyright 2007-2009 Inditel Meedia OÜ
 * @author Oliver Leisalu <oliver@inditel.ee>
 */
 
/**
* @package DomAr
* @subpackage Plugin
*/	
interface IDomArProperty_Plugin extends IDomArProperty, IDomArPropertyValidator, IDomArPropertyLoadListener, IDomArPropertyAlterTableListener {
	
	/**
	* @param String $type Listener type (beforeAdd, afterAdd, etc...)
	* @param String $method Method name that should be called for parent object when event occurs.
	* @param Array $args Method arguments that should be added to method calling.
	*/
	public function registerListener( $type, $method, array $args );
	
}

abstract class DomArProperty_Plugin extends DomArProperty implements IDomArProperty_Plugin{

	public function registerListener( $type, $method, array $args ) {
		throw new RuntimeException('<strong>'.get_class($this).'</strong> has no plugin listeners. Tried to add listener type <strong>'.$type.'</strong> which calls method <strong>'.$method.'</strong>');
	}
}

/**
* @package DomAr
* @subpackage Plugin
*/	
abstract class DomArPlugin {

	private $parent;
	private $property;

	public function __construct( DomArObject $parent = NULL, IDomArProperty_Plugin $property = NULL ) {
		$this->parent = $parent;
		$this->property = $property;
	}

	/**
	* This method is called when plugin object parent is changed. Default it removes plugin object from old parent and sets new property and parent. Doesn't affect new parent.
	* @param DomArObject $parent
	* @param IDomArProperty_Plugin $property
	* @return void
	*/
	public function changeParent( DomArObject $parent = NULL, IDomArProperty_Plugin $property = NULL) {
		$this->parent = $parent;
		$this->property = $property;
	}

	final public function getParent() {
		return $this->parent;
	}
	
	final public function getProperty() {
		return $this->property;
	}
	
	final public function changed() {
		if( $this->parent !== NULL )
			$this->parent->changed( $this->property->name );
	}
}

?>